package com.example.smart.ui.activity.home

import android.media.MediaPlayer
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.databinding.ActivityPreventionDetailsBinding
import com.example.smart.listener.CopyVideoListener
import com.example.smart.ui.m.MPreventionDetailsActivity
import com.example.smart.utils.FileUtils

class PreventionDetailsActivity : BaseTitleBarActivity<ActivityPreventionDetailsBinding>() {
    private val assets by lazy {
        getAssets().openFd("audio.m4a")
    }

    private var isStart = false

    private val mMedia = MediaPlayer().also {
        it.setOnPreparedListener { player -> player.start() }
    }

    override fun layoutID() = R.layout.activity_prevention_details

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@PreventionDetailsActivity,
            statusBarColor = com.example.lib.R.color.black_ececf8)
        title = getString(R.string.article)
        setAppBarColor(getColor(com.example.lib.R.color.black_ececf8))
    }

    override fun initListener() {
        binding.Lin1.setOnDoubleClickListener {
            FileUtils.copyVideo(this@PreventionDetailsActivity, "audio.m4a", object : CopyVideoListener {
                override fun onSuccess(path: String) {
                    if (isStart) {
                        if (mMedia.isPlaying) {
                            mMedia.pause()
                        } else {
                            mMedia.start()
                        }
                    } else {
                        isStart = true
                        mMedia.stop()
                        mMedia.reset()
                        mMedia.setDataSource(path)
                        mMedia.prepareAsync()
                    }
                }

                override fun onFail() {
                    ToastUtils.showToast("播放失败，请稍后重试！")
                }
            })

        }
    }

    override fun onDestroy() {
        mMedia.stop()
        mMedia.release()
        super.onDestroy()
    }

}
